import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({
    name: 'safe'
})

export class SafeUrlPipe implements PipeTransform {

    constructor(private san: DomSanitizer) { }

    transform(value: string, ext: string) {
        switch (ext) {
            case 'resourceUrl': return this.san.bypassSecurityTrustResourceUrl(value);  // <script src> 或 <iframe src>
            case 'html': return this.san.bypassSecurityTrustHtml(value); // html
            case 'style': return this.san.bypassSecurityTrustStyle(value); // css
            case 'script': return this.san.bypassSecurityTrustScript(value); // js
            case 'url': return this.san.bypassSecurityTrustUrl(value); // <a href> <img src>

            default: return this.san.bypassSecurityTrustUrl(value); // <a href> <img src>
        }

    }
}
